package im.xz.cn.tpplayer1206;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
public class tpp implements CommandExecutor {
    Plugin plugin = im.xz.cn.tpplayer1206.TpPlayer_1206.getPlugin(im.xz.cn.tpplayer1206.TpPlayer_1206.class);
    @Override
    public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
        try {
            if (commandSender instanceof Player) {
                Player player = (Player) commandSender;
                try {
                    Boolean needPermission = plugin.getConfig().getBoolean("needPermission");
                    if (!needPermission) {
                        try {
                            if (strings.length == 1) {
                                String target = strings[0];
                                String name = player.getName();
                                try {
                                    if (target.equalsIgnoreCase(name)) {
                                        String cantSelf = plugin.getConfig().getString("cantSelf");
                                        player.sendMessage(ChatColor.RED + cantSelf);
                                        return false;
                                    }
                                } catch (NullPointerException ignore) {
                                }
                                for (Object o : player.getServer().getOnlinePlayers().toArray()) {
                                    try {
                                        Player awa = (Player) o;
                                        if ((awa.getName().toString().equalsIgnoreCase(target))) {
                                            Player targetPlayer = (Player) o;
                                            targetPlayer.sendMessage(ChatColor.WHITE + player.getName() + ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("notice")));
                                            try {
                                                if (!targetPlayer.getInventory().contains(Material.AIR)) {
                                                    try {
                                                        for (int i = 0; i <= 35; i++) {
                                                            ItemStack item1 = new ItemStack(Material.PAPER);
                                                            ItemMeta meta = item1.getItemMeta();
                                                            meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("itemName")));
                                                            meta.setEnchantmentGlintOverride(true);
                                                            ArrayList<String> lore = new ArrayList<>();
                                                            lore.add(ChatColor.WHITE + "操作使用以接受传送，扔掉拒绝传送");
                                                            lore.add("请求来自 ");
                                                            lore.add(name);
                                                            meta.setLore(lore);
                                                            item1.setItemMeta(meta);
                                                            targetPlayer.getInventory().addItem(item1);
                                                            player.sendMessage(ChatColor.GREEN + ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("sendSuccessMessage")));
                                                            break;
                                                        }
                                                    } catch (NullPointerException ignore) {
                                                    }
                                                } else {
                                                    targetPlayer.sendMessage(ChatColor.RED + ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("fullMessage")));
                                                }
                                            } catch (NullPointerException ignore) {

                                            }
                                            break;
                                        } else {
                                        }
                                    } catch (NullPointerException ignore) {
                                    }
                                }
                            } else {
                                commandSender.sendMessage(ChatColor.RED + "语法错误,请参考： /tpp <目标玩家名字>");
                                commandSender.sendMessage(ChatColor.AQUA + "如需帮助，请参阅https://im.xz.cn/index.php/archives/59/");
                            }
                        } catch (NullPointerException ignore) {
                        }
                    }else {
                        if (player.hasPermission("xzrui.tpplayer.command.tpp")) {
                            try {
                                if (strings.length == 1) {
                                    String target = strings[0];
                                    String name = player.getName();
                                    try {
                                        if (target.equalsIgnoreCase(name)) {
                                            String cantSelf = plugin.getConfig().getString("cantSelf");
                                            player.sendMessage(ChatColor.RED + cantSelf);
                                            return false;
                                        }
                                    } catch (NullPointerException ignore) {
                                    }
                                    for (Object o : player.getServer().getOnlinePlayers().toArray()) {
                                        try {
                                            Player awa = (Player) o;
                                            if ((awa.getName().toString().equalsIgnoreCase(target))) {
                                                Player targetPlayer = (Player) o;
                                                targetPlayer.sendMessage(ChatColor.WHITE + player.getName() + ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("notice")));
                                                try {
                                                    if (!targetPlayer.getInventory().contains(Material.AIR)) {
                                                        try {
                                                            for (int i = 0; i <= 35; i++) {
                                                                ItemStack item1 = new ItemStack(Material.PAPER);
                                                                ItemMeta meta = item1.getItemMeta();
                                                                meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("itemName")));
                                                                meta.setEnchantmentGlintOverride(true);
                                                                ArrayList<String> lore = new ArrayList<>();
                                                                lore.add(ChatColor.WHITE + "操作使用以接受传送，扔掉拒绝传送");
                                                                lore.add("请求来自 ");
                                                                lore.add(name);
                                                                meta.setLore(lore);
                                                                item1.setItemMeta(meta);
                                                                targetPlayer.getInventory().addItem(item1);
                                                                player.sendMessage(ChatColor.GREEN + ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("sendSuccessMessage")));
                                                                break;
                                                            }
                                                        } catch (NullPointerException ignore) {
                                                        }
                                                    } else {
                                                        targetPlayer.sendMessage(ChatColor.RED + ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("fullMessage")));
                                                    }
                                                } catch (NullPointerException ignore) {
                                                }
                                                break;
                                            } else {
                                            }
                                        } catch (NullPointerException ignore) {
                                        }
                                    }
                                } else {
                                    commandSender.sendMessage(ChatColor.RED + "语法错误,请参考： /tpp <目标玩家名字>");
                                    commandSender.sendMessage(ChatColor.AQUA + "如需帮助，请参阅https://im.xz.cn/index.php/archives/59/");
                                }
                            } catch (NullPointerException ignore) {
                            }
                        } else {
                            player.sendMessage(ChatColor.RED + "权限不足！如果你确认这是不应该的，请联系管理员并让他检查你是否有xzrui.tpplayer.command.tpp权限");
                        }
                    }
                }catch (NullPointerException ignore) {
                }
            }else {
                System.out.println("this command can only be ran by player");
            }
        }catch (NullPointerException ignore) {
        }
        return false;
    }
}